package rinpu.springtest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import rinpu.springtest.mapper.UserMapper;
import rinpu.springtest.pojo.dto.RegisterDto;
import rinpu.springtest.pojo.entity.User;
import rinpu.springtest.utils.StringUtils;

/**
 * @Author Rin_pu
 * @Date 2025/9/24 21:22
 * @Description RegisterServiceImpl
 */
@Service
public class RegisterServiceImpl {

    @Resource
    private UserMapper userMapper;

    public int register(@Valid RegisterDto registerDto) {
        String username = registerDto.getUsername();
        String password = new BCryptPasswordEncoder().encode(registerDto.getPassword());
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, username));
        if (StringUtils.isNotNull(user)) {
            throw new RuntimeException("账号已存在");
        }
        User insert = new User();
        insert.setUsername(username);
        insert.setPassword(password);
        return userMapper.insert(insert);
    }
}
